package com.mh.view;

import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import com.mh.entity.User;
import com.mh.jdbc.util.StringUtil;
import com.mh.service.UserService;

public class LoginOnFrame extends JFrame {
	private static final long serialVersionUID = 65580778678900296L;
	private JTextField username;
	private JPasswordField password;
	
	private UserService userService = new UserService();
	/**
	 * Create the frame.
	 */
	public LoginOnFrame() {
		setAlwaysOnTop(true);
		setName("login");
		setType(Type.POPUP);
		setResizable(false);
		setForeground(Color.LIGHT_GRAY);
		setIconImage(Toolkit.getDefaultToolkit().getImage(LoginOnFrame.class.getResource("/com/mh/images/logo.png")));
		setTitle("图书管理系统");
		setSize(450, 378);
		setLocationRelativeTo(null);
		JPanel contentPane = new JPanel();
		contentPane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblNewLabel = new JLabel("图书管理系统");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		lblNewLabel.setIcon(new ImageIcon(LoginOnFrame.class.getResource("/com/mh/images/loginlogo.png")));
		
		JLabel lblNewLabel_1 = new JLabel("用户名:");
		lblNewLabel_1.setIcon(new ImageIcon(LoginOnFrame.class.getResource("/com/mh/images/username.png")));
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 18));
		
		JLabel lblNewLabel_2 = new JLabel("密  码:");
		lblNewLabel_2.setIcon(new ImageIcon(LoginOnFrame.class.getResource("/com/mh/images/password.png")));
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 18));
		
		username = new JTextField();
		username.setToolTipText("用户名");
		username.setFont(new Font("宋体", Font.PLAIN, 18));
		username.setColumns(10);
		password = new JPasswordField();
		password.setFont(new Font("宋体", Font.PLAIN, 18));
		
		JButton login = new JButton("登录");
		login.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				loginActionPerformed(e);
			}
		});
		login.setFont(new Font("宋体", Font.PLAIN, 20));
		login.setIcon(new ImageIcon(LoginOnFrame.class.getResource("/com/mh/images/login.png")));
		
		JButton reset = new JButton("重置");
		reset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetValueActionPerformed(e);
			}
		});
		reset.setIcon(new ImageIcon(LoginOnFrame.class.getResource("/com/mh/images/reset.png")));
		reset.setFont(new Font("宋体", Font.PLAIN, 20));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(65)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
									.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
									.addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE))
								.addComponent(login))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(username, GroupLayout.PREFERRED_SIZE, 175, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
									.addComponent(reset)
									.addComponent(password, GroupLayout.PREFERRED_SIZE, 176, GroupLayout.PREFERRED_SIZE))))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(118)
							.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 184, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(71, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE)
					.addGap(25)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(username, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
					.addGap(30)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(password, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
					.addGap(52)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(reset)
						.addComponent(login))
					.addContainerGap(41, Short.MAX_VALUE))
		);
		contentPane.setLayout(gl_contentPane);
	}
	//登录
	protected void loginActionPerformed(ActionEvent evt) {
		String username = this.username.getText();
		String password = new String(this.password.getPassword());
		if(StringUtil.isEmpty(username)){
			JOptionPane.showMessageDialog(null, "用户名不能为空");
			return;
		}
		if(StringUtil.isEmpty(password)){
			JOptionPane.showMessageDialog(null, "密码不能为空");
			return;
		}
		User loginUser= userService.login(username, password);
		if(null != loginUser){
			JOptionPane.showMessageDialog(null, "登录成功");
			dispose();
			new MainFrame().setVisible(true);
		}else{
			JOptionPane.showMessageDialog(null, "用户名或者密码错误");
		}
	}

	/**
	 * 重置时间处理
	 * @param evt
	 */
	protected void resetValueActionPerformed(ActionEvent evt) {
		this.username.setText("");
		this.password.setText("");
	}
}
